package com.study.selection.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.selection.model.College;
import com.study.selection.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/college")
public class CollegeController {
    // 注入Service
    @Autowired
    private CollegeService collegeService;
    // 查询所有,绑定数据，转发到college.jsp
    @RequestMapping("/listAll")
    public String doList(@RequestParam(value = "page",defaultValue = "1") Integer page, Model model){
        //添加分页，page表示查询第几页
        PageHelper.startPage(page,5);
        List<College> list = collegeService.findAll();
        PageInfo<College> info = new PageInfo<>(list,3);
        // 向页面返回封装后的数据
        model.addAttribute("info",info);
        return "/view/admin/college.jsp";
    }
    // 根据关键字查询,绑定数据，转发到college.jsp
    @RequestMapping("/search")
    public String doSearch(@RequestParam(value = "page",defaultValue = "1")Integer page,String key,Model model){
        PageHelper.startPage(page,5);
        List<College> list = collegeService.findByKey(key);
        PageInfo<College> info = new PageInfo<>(list,3);
        model.addAttribute("info",info);
        model.addAttribute("key",key);
        return "/view/admin/college.jsp";
    }
   // 添加对象,接收表单参数,执行添加，重定向到listAll
    @RequestMapping("/add")
    public String doAdd(College college){
        boolean result = collegeService.addCollege(college);
        // 可判断是成功还是失败
        // 再做查询
        return "redirect:listAll";
    }
    // 根据主键获取单个对象，转发到edit_college.jsp
    @RequestMapping("/getOne")
    public String doGetOne(int collegeId,Model model){
        College college = collegeService.getById(collegeId);
        model.addAttribute("college",college);
        return "/view/admin/edit_college.jsp";
    }

//    // 修改对象，接收表单参数，执行修改，重定向到listAll
    @RequestMapping("/update")
    public String doUpdate(College college){
        boolean result=collegeService.updateCollege(college);
        if (result){
            return "redirect:listAll";
        }else {
            System.out.println(result);
        }
        return "/view/admin/edit_college.jsp";

    }
//  // 删除对象，接收请求参数id,执行删除，重定向到listAll
    @RequestMapping("/delete")
    public String doDelete(int collegeId){
        boolean result=collegeService.deleteCollege(collegeId);
        return "redirect:listAll";
    }
    @RequestMapping("/deleteItems")
    public String deleteItems(String[] chkIds) {
        for (String string : chkIds) {
           collegeService.deleteCollege(Integer.parseInt(string));
        }
        return "redirect:listAll";
    }

}



